package com.ita.tba.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import com.ita.tba.dao.CartDao;
import com.ita.tba.po.Cart;
import com.ita.tba.util.DbUtil;
import com.ita.tba.util.DidNumUtil;

public class CartDaoImpl implements CartDao{

	@Override
	public Cart findCart(int uid) {
		Cart cart = new Cart();
		String sql = "select * from tbacart where userid=?";

		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, uid);
			rs = pst.executeQuery();
			while(rs.next()) {
				cart.setUid(uid);
				cart.setTotalPrice(rs.getDouble("total"));
				String didNumStr = rs.getString("didnum");
				Map<Integer, Integer> didNumMap = DidNumUtil.didNumStrToMap(didNumStr);
				cart.setDidNum(didNumMap);
			}
			

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, rs);
		}

		return cart;
	}

	@Override
	public int updateCart(int uid, Map<Integer, Integer> didNumTemp, double totalPrice) {
		String sql = "update tbacart set didnum=?,total=? where userid=?";
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		String didNumStr = DidNumUtil.didNumMapToStr(didNumTemp);
		
		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setString(1, didNumStr);
			pst.setDouble(2, totalPrice);
			pst.setInt(3, uid);
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	@Override
	public int addCart(Cart cart) {
		String sql = "insert into tbacart(userid,didnum,total) values (?,?,?)";
		
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		String didNumStr = DidNumUtil.didNumMapToStr(cart.getDidNum());
		 
		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);	
			pst.setInt(1, cart.getUid());
			pst.setString(2, didNumStr);
			pst.setDouble(3, cart.getTotalPrice());			
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	@Override
	public int deleteCart(int uid) {
		String sql = "delete from tbacart where userid=?";

		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();

		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setInt(1, uid);
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

}
